﻿using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace CoreTest;

public partial class TagMonitor : Window
{
    public TagMonitor()
    {
        InitializeComponent();
    }

    bool _isSim = false;
    ObservableCollection<TagItem> list = new ObservableCollection<TagItem>();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var metalist = App.Server.MetaDataList;
        for (int i = 0; i < metalist.Count; i++)
        {
            list.Add(new TagItem(metalist[i]));
        }
        list1.ItemsSource = list;
    }

    private void Window_Closed(object sender, EventArgs e)
    {
        foreach (var tag in list)
        {
            tag.Dispose();
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TagItem tag = list1.SelectedItem as TagItem;
        if (tag != null)
        {
            if (_isSim) tag.SimWrite(textBox1.Text); else tag.Write(textBox1.Text);
        }
        childWindow1.Close();
    }

    private void MenuWrite_Click(object sender, RoutedEventArgs e)
    {
        _isSim = false;
        textBox1.Focus();
        childWindow1.Show();
    }

    private void MenuSimWrite_Click(object sender, RoutedEventArgs e)
    {
        _isSim = true;
        textBox1.Focus();
        childWindow1.Show();
    }
}
